import 'package:flutter/material.dart';
import 'package:flutterhello/util/host_util.dart';
import 'package:get/get.dart';

class ActDetail extends StatefulWidget {
  @override
  _ActDetailState createState() => _ActDetailState();
}

class _ActDetailState extends State<ActDetail> {
  var info;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.info = Get.arguments;
    print(info);
  }

  imageList() {
    List<Widget> list = [];
    info['images'].forEach((item) {
      list.add(Container(
        height: double.infinity,
        width: double.infinity,
        decoration: BoxDecoration(
            image: DecorationImage(
                image: NetworkImage(HostUtil.getHost() + item),
                fit: BoxFit.fitWidth)),
      ));
    });
    return list;
  }

  vipList() {
    List<Widget> list = [];
    if (this.info['items'].length == 0) {
      return Padding(
        padding: EdgeInsets.only(left: 20, right: 20),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text('暂无人员参与', style: TextStyle(color: Colors.white)),
            GestureDetector(
              child: Container(
                width: 50,
                height: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(25)),
                  border: Border.all(color: Colors.white, width: 2),
                ),
                child: Center(
                  child: Icon(
                    Icons.more_horiz,
                    color: Colors.white,
                  ),
                ),
              ),
              onTap: () {
                Get.toNamed('/act_team_list');
              },
            )
          ],
        ),
      );
    } else {
      var showItem = [];
      if (this.info['items'].length > 4) {
        showItem = this.info['items'].sublist(0, 4);
      } else {
        showItem = this.info['items'];
      }
      showItem.forEach((element) {
        print(element);
        list.add(
          Padding(
            padding: EdgeInsets.only(right: 17),
            child: Container(
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(25)),
                  border: Border.all(color: Colors.white, width: 2),
                  image: DecorationImage(
                      image: NetworkImage(
                          HostUtil.getHost() + element['favicon']))),
            ),
          ),
        );
      });
      return Padding(
        padding: EdgeInsets.only(left: 20, right: 20),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Row(
                children: list,
              ),
            ),
            GestureDetector(
              child: Container(
                width: 50,
                height: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(25)),
                  border: Border.all(color: Colors.white, width: 2),
                ),
                child: Center(
                  child: Icon(
                    Icons.more_horiz,
                    color: Colors.white,
                  ),
                ),
              ),
              onTap: () {
                Get.toNamed('/act_team_list', arguments: this.info['items']);
              },
            )
          ],
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(color: Colors.black),
        child: Stack(
          children: [
            Align(
              alignment: Alignment(0, 0),
              child: PageView(
                scrollDirection: Axis.vertical,
                controller: PageController(),
                children: imageList(),
              ),
            ),
            Align(
              alignment: Alignment(0, -0.9),
              child: Padding(
                padding: EdgeInsets.only(left: 15, right: 15),
                child: Container(
                  width: double.infinity,
                  height: 60,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButton(
                        child: Container(
                          height: 40,
                          width: 40,
                          decoration: BoxDecoration(
                              color: Color.fromARGB(150, 100, 100, 100),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(8))),
                          child: Center(
                            child: Icon(
                              Icons.chevron_left,
                              color: Colors.white,
                              size: 30,
                            ),
                          ),
                        ),
                        onPressed: () {
                          Get.back();
                        },
                      ),
                      Row(
                        children: [
                          CircleAvatar(
                            //头像半径
                            radius: 16,
                            //头像图片
                            backgroundImage: info['group'].length != 0
                                ? NetworkImage(HostUtil.getHost() +
                                    info['group'][0]['favicon'])
                                : NetworkImage(HostUtil.getHost() +
                                    info['user'][0]['favicon']),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Container(
                            child: Text(
                              info['group'].length != 0
                                  ? info['group'][0]['groupname']
                                  : info['user'][0]['username'],
                              style:
                                  TextStyle(fontSize: 16, color: Colors.white),
                            ),
                          ),
                        ],
                      ),
                      GestureDetector(
                        child: Container(
                          height: 40,
                          width: 40,
                          decoration: BoxDecoration(
                              color: Color.fromARGB(150, 100, 100, 100),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(8))),
                          child: Center(
                            child: Icon(
                              Icons.chat,
                              color: Colors.white,
                              size: 20,
                            ),
                          ),
                        ),
                        onTap: () {
                          // Get.toNamed('/chat_detail');
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Align(
              alignment: Alignment(-0.7, 0.7),
              child: Padding(
                padding: EdgeInsets.only(left: 15, right: 15),
                child: Container(
                  width: double.infinity,
                  height: 170,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: 300,
                        margin: EdgeInsets.only(
                          left: 10,
                          right: 10,
                        ),
                        child: Text(
                          info['title'],
                          style: TextStyle(color: Colors.white, fontSize: 25),
                          maxLines: 2,
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Padding(
                        padding: EdgeInsets.only(
                          left: 10,
                          right: 10,
                        ),
                        child: Text(
                          info['content'],
                          maxLines: 2,
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      SizedBox(
                        height: 15,
                      ),
                      Container(
                        width: 360,
                        height: 66,
                        decoration: BoxDecoration(
                            color: Color.fromARGB(150, 100, 100, 100),
                            borderRadius:
                                BorderRadius.all(Radius.circular(30))),
                        child: vipList(),
                      )
                    ],
                  ),
                ),
              ),
            ),
            Align(
              alignment: Alignment(0, 0.9),
              child: Container(
                width: 320,
                height: 50,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '参与活动',
                      style: TextStyle(color: Colors.white),
                    ),
                    Container(
                      height: 50,
                      width: 50,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(25)),
                        color: Colors.white,
                      ),
                      child: IconButton(
                          onPressed: () {
                            Get.toNamed('/join_act', arguments: info);
                          },
                          icon: Icon(
                            Icons.arrow_forward,
                            color: Colors.blueGrey,
                          )),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
